package com.atguigu.day15.stringbudder;

import org.junit.Test;

/*
StringBuffer vs StringBuilder:
    同 : 共同的父亲 AbstractStringBuilder
        可变的字符序列
        char[]  默认长度 16
   不同:
     1.线程安全问题
        StringBuffer:线程安全 效率低
        StringBuilder:线程不安全 效率高
     2.StringBuffer jdk1.0
      StringBuilder jdk1.5





 */
public class StringBufferTest {
    @Test
    public void test06() {
        StringBuffer sbf = new StringBuffer();

        for (char c = 'A'; c <= 'Z'; c++) {
            sbf.append(c);
        }
    }


    @Test
    public void test01() {
        StringBuffer sbf = new StringBuffer();
    }

    @Test
    public void test02() {
        StringBuffer sbf = new StringBuffer("ABCD");
        System.out.println("sbf.length() = " + sbf.length());
        sbf.setLength(2);
        System.out.println("sbf = " + sbf);
    }

    @Test
    public void test03() {
        StringBuffer sbf = new StringBuffer("杨幂");
        System.out.println("sbf = " + sbf);
        //追加
        sbf.append("赵丽颖");
        System.out.println("sbf = " + sbf);//杨幂赵丽颖
        sbf.append(3.14);
        sbf.append(666);
        System.out.println("sbf = " + sbf);//杨幂赵丽颖3.14666
        //在指定位置插入
        sbf.insert(1, "妲己");
        System.out.println("sbf = " + sbf);//杨妲己幂赵丽颖3.14666
        //删除指定下标的数据
        sbf.deleteCharAt(6);
        System.out.println("sbf = " + sbf);
        //删除范围数据[开始,结束)
        sbf.delete(0, 5);
        System.out.println("sbf = " + sbf);//丽3.14666
        //修改指定下标的值
        sbf.setCharAt(3, '你');
        System.out.println("sbf = " + sbf);//丽3.你4666

        int indexedOf = sbf.indexOf("你");
        System.out.println("indexedOf = " + indexedOf);//3

    }

    @Test
    public void test04() {
        StringBuffer sbf = new StringBuffer("ABCD");
        sbf.replace(0, 2, "赵丽颖");
        System.out.println("sbf = " + sbf);//赵丽颖CD

        sbf.reverse();

        System.out.println("sbf = " + sbf);//DC颖丽赵

        //将StringBuffer --> String
        String s = sbf.toString();
        System.out.println("s = " + s);


    }
}
